package com.ymatou.apigateway.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.GetOrderStatusReq;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.ShenqiGetOrderListCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_sqgetordesStatus {
	
	private static OpenapiReq openapiReq;
	private static ShenqiGetOrderListCall shenqiGetOrderListCall;
	private static OpenApiWapper openApiWapper;
	private static GetOrderStatusReq getOrderStatusReq;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("openapi获取神器订单状态");
		//stag 环境注释掉
		openApiWapper = new OpenApiWapper();

	}

	@Before
	public void caseUp() {

		openapiReq = new OpenapiReq();
		getOrderStatusReq = new GetOrderStatusReq();
		shenqiGetOrderListCall = new ShenqiGetOrderListCall();
		openapiReq.setMethod("sq.orders.status.get");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shenqiGetOrderListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqgetordesStatus_001() {
		Logger.start(true, "openapi获取订单详情 指定orderid");
		try {

			long orderId = 3741928699L;

			long orderId2 = 3750648699L;

			long sellerId = 500004881L;

			getOrderStatusReq.setOrderIds(Arrays.asList(orderId, orderId2));

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getOrderStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.orders.status.get",
							app_id);

			shenqiGetOrderListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			shenqiGetOrderListCall.setData(openapiReq);

			shenqiGetOrderListCall.callService();

			Logger.verifyEquals("0000", shenqiGetOrderListCall.getCode(), "校验code");
			Logger.verifyEquals("", shenqiGetOrderListCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(shenqiGetOrderListCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqgetordesStatus_002() {
		Logger.start(true, "openapi 订单支付,获取订单详情");
		try {

			long orderId = 1420019405;

			long sellerId = 500002490;

			getOrderStatusReq.setOrderIds(Arrays.asList(orderId));

			Logger.comment("userid:" + sellerId);
			Application application = openApiWapper
					.selectApplicationBySellerId(Long.valueOf(sellerId)).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getOrderStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.orders.status.get",
							app_id);

			shenqiGetOrderListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			shenqiGetOrderListCall.setData(openapiReq);

			shenqiGetOrderListCall.callService();

			Logger.verifyEquals("0000", shenqiGetOrderListCall.getCode(), "校验code");
			Logger.verifyEquals("", shenqiGetOrderListCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(shenqiGetOrderListCall.getContent(), "校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqgetordesStatus_003() {
		Logger.start(true, "openapi获取订单详情,该订单不是买手的订单");
		try {

			getOrderStatusReq.setOrderIds(Arrays.asList(1420039407L));

			Application application = openApiWapper
					.selectApplicationBySellerId(500004881L).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getOrderStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.orders.status.get",
							app_id);

			shenqiGetOrderListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			shenqiGetOrderListCall.setData(openapiReq);

			shenqiGetOrderListCall.callService();

			Logger.verifyEquals("0000", shenqiGetOrderListCall.getCode(), "校验code");
			Logger.verifyEquals("", shenqiGetOrderListCall.getMsg(),
					"校验message");

			DataUtil.verify("[]",
					shenqiGetOrderListCall.getContent().get("orders_status"),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_sqgetordesStatus_004() {
		Logger.start(true, "openapi获取订单详情,订单号不存在");
		try {

			/*			PlaceOrderResp placeOrderResp = PlaceOrderService
								.placeOrderWithOutCoupon();

						int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
						int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			*/

			getOrderStatusReq.setOrderIds(Arrays.asList(11111111L));

			Application application = openApiWapper
					.selectApplicationBySellerId(500004881L).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(getOrderStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.orders.status.get",
							app_id);

			shenqiGetOrderListCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			shenqiGetOrderListCall.setData(openapiReq);

			shenqiGetOrderListCall.callService();

			Logger.verifyEquals("0000", shenqiGetOrderListCall.getCode(), "校验code");
			Logger.verifyEquals("", shenqiGetOrderListCall.getMsg(),
					"校验message");

			DataUtil.verify(null,
					shenqiGetOrderListCall.getContent().get("orders_status"),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
